package cn.ym.mall.service.impl;

import cn.ym.mall.dao.RoleDao;
import cn.ym.mall.dao.impl.RoleDaoImpl;
import cn.ym.mall.entity.Role;
import cn.ym.mall.service.RoleService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    RoleDao roleDao = new RoleDaoImpl();
    /**
     * 获得所有角色信息
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Role> getAll() throws SQLException {
        Connection conn = null;
        List<Role> roleList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            roleList = roleDao.getAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return roleList;
    }

    /**
     * 添加角色信息
     *
     * @param role
     * @return
     * @throws SQLException
     */
    @Override
    public boolean add(Role role) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);// 关闭自动提交
            if (roleDao.add(role) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据角色Id删除角色信息
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean delete(Integer rid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);// 关闭自动提交
            if (roleDao.delete(rid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 更新角色信息
     *
     * @param role
     * @return
     * @throws SQLException
     */
    @Override
    public boolean update(Role role) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);// 关闭自动提交
            if (roleDao.update(role) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据角色编号获得角色信息
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public Role getRoleByRid(Integer rid) throws SQLException {
        Connection conn = null;
        Role role = null;
        try {
            conn = DruidUtils.getConnection();
            role = roleDao.getRoleByRid(rid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return role;
    }
}
